﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ElingOA6_Admin_DSJCJ.Common
{
    /// <summary>
    /// 系统管理基类
    /// </summary>
    public class AdminBaseController : Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //filterContext.HttpContext.Response.AddHeader("X-UA-Compatible", "IE=7");
            //ASPxWebControl.SetIECompatibilityMode(7);
            //#if DEBUG
            //            OperateSession.add(GlobalConstant.PARAM_LOGINID, "admin");
            //            OperateSession.add(GlobalConstant.PARAM_USERNAME, "超级管理员");
            //#endif
            //这里判断是否登录

            

            if (string.IsNullOrEmpty(OperateSession.get(GlobalConstant.PARAM_LOGINID)))
            {
               // Response.Write("<script>window.alert('对不起，请重新登陆！');window.top.location.replace('" + Url.Action("Index", "Login") + "')</script>");
                Response.Write("<script>window.alert('对不起，请重新登陆！');window.top.location.replace('" + Url.Content("~/Login") + "')</script>");
               
                Response.Flush();
                Response.End();
            }
        }
    }

    /// <summary>
    /// 菜单是否授权判断
    /// </summary>
    public class AuthorityActionFilter : ActionFilterAttribute
    {
        /// <summary>
        /// 菜单编码
        /// </summary>
        public string MnuName;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mnuName">菜单编码</param>
        public AuthorityActionFilter(string mnuName)
        {
            MnuName = mnuName;
        }
        /// <summary>
        /// 在action method之前执行
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!string.IsNullOrEmpty(OperateSession.get(GlobalConstant.PARAM_LOGINID)))
            {
                bool isAuthority = false;
                if (MnuName != "")
                {
                    List<Model.PrvFunctList> prvFunctListList = OperateSession.getList<Model.PrvFunctList>(GlobalConstant.PARAM_PRVFUNCTIONLIST);
                    var prvList = prvFunctListList.Where(s => s.FFunctNo == MnuName);
                    if (prvList.ToList().Count() > 0)
                    {
                        isAuthority = true;
                    }
                }

                if (!isAuthority)
                {
                    filterContext.Controller.ViewBag.Error = "未授权";
                    filterContext.HttpContext.Response.Write("未授权");
                    filterContext.HttpContext.Response.Flush();
                    filterContext.HttpContext.Response.End();
                }
            }
        }

    }
    /// <summary>
    /// 记录页面日志
    /// </summary>
    public class LogPageActionFilter : ActionFilterAttribute
    {
        public string PageTitle = "";
        /// <summary>
        /// 页面名称
        /// </summary>
        /// <param name="pageTitle"></param>
        public LogPageActionFilter(string pageTitle)
        {
            PageTitle = pageTitle;
        }
        /// <summary>
        /// 在action method之前执行
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string note = "";
            if (filterContext.Controller.ViewBag.Error != null)
            {
                note = filterContext.Controller.ViewBag.Error;
            }

            string actionParams = "";
            foreach (KeyValuePair<string, object> actionParam in filterContext.ActionParameters)
            {
                actionParams += actionParam.Key + "=" + actionParam.Value + ",";
            }
            if (actionParams != "")
            {
                actionParams = actionParams.Substring(0, actionParams.LastIndexOf(','));
            }
            OperateLogHelper.AddFunctionOperateLog("进入了" + PageTitle + "页面", actionParams, note);
        }

    }
}